<!-- eslint-disable prettier/prettier -->
<!-- 用户编辑弹窗 -->
<template>
	<ele-modal
		:width="850"
		:visible="visible"
		:confirm-loading="loading"
		title="选择货品"
		:body-style="{ paddingBottom: '8px' }"
		@update:visible="updateVisible"
		@ok="save"
	>
		<a-form :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }" :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }">
			<a-row :gutter="24">
				<a-col :md="24" :sm="24" :xs="24">
					<zbTable ref="tableRef" :initLoad="initLoad" :api-fun="get_goods_list" @getselection="getselection">
						<!-- <template v-slot:optBtn="{ record }">
							<a-space>
								<a @click="selAction(record)">选择</a>
							</a-space>
						</template> -->
					</zbTable>
				</a-col>
			</a-row>
		</a-form>
	</ele-modal>
</template>

<script setup>
  import { ref, unref, reactive, onMounted } from 'vue';
  import { Form } from 'ant-design-vue';
  import { message, Modal } from 'ant-design-vue';
  import { emailReg, phoneReg, identityReg } from 'ele-admin-pro';
  import { useRouter } from 'vue-router';
  import zbTable from '@/components/zbTable/jewelrySelIndex.vue';
  import { get_goods_list } from '@/api/jewelry/receipts_distribute';
  import { add_goods } from '@/api/jewelry/receipts_allocation';

  const tableRef = ref(null);
  // 表格列配置
  const useForm = Form.useForm;

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean
  });

  const emit = defineEmits(['update:visible', 'load']);
  const { currentRoute, push } = useRouter();
  const { query } = unref(currentRoute);
  console.log(query);

  // 提交状态
  const loading = ref(false);
  const initLoad = ref(false);
  const form = reactive({
    code: '',
    goods_code: []
  });

  onMounted(() => {
    form.code = query.code;
    setTimeout(() => {
      tableRef.value.changeSelection('1');
    }, 800);
  });

  const getselection = (val) => {
    form.goods_code = val.map((item) => {
      return item.code;
    });
    console.log(form);
  };
  const selAction = () => {};
  const save = () => {
    add_goods(form)
      .then((res) => {
        message.success(res.msg);
        reloadTable();
        emit('load');
      })
      .catch((e) => {
        message.error(e);
      });
  };
  const reloadTable = () => {
    updateVisible(false);
    tableRef.value.reload();
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
